package com.nhjf.admin.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.nhjf.admin.common.util.SysProperties;

import net.sf.json.JSONObject;
 
 
/**
 * 上传图片的controller
 * @author lixy
 *
 */
@Controller
@RequestMapping(value="/resource/upload")
public class UploadimageController {
	private static SimpleDateFormat dirFormatter;// 目录命名格式:yyyyMM
	
	/**
	 * 上传图片
	 * @param file
	 * @param request
	 * @param response
	 * @return MultipartFile uploadFile,
	 */
	@ResponseBody
	@RequestMapping(value="/images", produces="application/html;charset=UTF-8")
	public void images (@RequestParam(value = "upfile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
		Map<String, String> params = new HashMap<String, String>();
		response.setContentType("application/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		dirFormatter = new SimpleDateFormat("yyyyMM");
		if (file == null){
            System.out.println("未获得上传文件!");			
//			return null;
		}  
		try{
			 Date dNow = new Date();
			 String filetype = request.getParameter("type")+"";
			 System.out.println("filetype:"+filetype);
			 String basePath =  LoadPropertiesDataUtils.getValue("toragesPath");
			 String visitUrl = LoadPropertiesDataUtils.getValue("visitPath");
			 if(basePath == null || "".equals(basePath)){
				 basePath = LoadPropertiesDataUtils.getValue("toragesPath");
			 } 
			 if(visitUrl == null || "".equals(visitUrl)){
				 visitUrl = "/upload/"; //与properties文件中lyz.visit.url相同，未读取到文件数据时为visitUrl赋默认值
			 }
			 if(basePath == null || "".equals(basePath)){
				 basePath =LoadPropertiesDataUtils.getValue("toragesPath");  //与properties文件中lyz.uploading.url相同，未读取到文件数据时为basePath赋默认值
			 }
			 System.out.println("filename:"+file.getOriginalFilename());
			 String ext = StringUtils.getExt(file.getOriginalFilename());
			 String fileName = String.valueOf(System.currentTimeMillis()).concat("_").concat(RandomUtils.getRandom(6)).concat(".").concat(ext);
			 StringBuilder sb = new StringBuilder();
			 //拼接保存路径
			 sb.append(basePath).append("//").append(fileName);
			 basePath= SysProperties.getInstance().get("editfileUploadPath")+File.separator+"fileUpload"+File.separator+"other"+File.separator+"Image"+File.separator+dirFormatter.format(dNow);           
			 sb.append(basePath);
			 visitUrl = visitUrl.concat(fileName);
			 File f = new File(basePath);
			 if(!f.exists()){
				 f.mkdir();
			 }
			 String currentPath = "/other/" + "Image" + "/"+ dirFormatter.format(dNow);
			 String currentWebPath=SysProperties.getInstance().get("editimagePath")+currentPath;
			 String file2= f+"//"+fileName;
			 File pathToSave = new File(currentWebPath, fileName);
			 OutputStream out = new FileOutputStream(file2);
			 FileCopyUtils.copy(file.getInputStream(),out);
			 PrintWriter outs = response.getWriter();
			 
			 params.put("state", "SUCCESS");
			 params.put("title", fileName);

			 params.put("original", file.getOriginalFilename());
			 params.put("type", ext);
			 params.put("url", currentWebPath+"/"+fileName);
			 params.put("size", file.getSize()+"");
			outs.println(JSONObject.fromObject(params));
			outs.flush();
			outs.close();
			 System.out.println("url:"+visitUrl+" original:"+fileName+" filename:"+file.getOriginalFilename()+" type:"+file.getContentType());
		} catch (Exception e){
			 params.put("state", "ERROR");
			 e.printStackTrace();
		}
//		 return params;
	}
}